package com.lagou.server;

import com.lagou.Exception.AgeException;
import com.lagou.model.Exam;
import com.lagou.model.ExamMessage;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;

    /**
     * 通过构造方法实现成员变量的初始化
     *
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public boolean serverReceive() throws IOException, ClassNotFoundException, AgeException {
        input();
        Object object = sic.getOis().readObject();
        if (object instanceof UserMessage) {
            UserMessage tum = (UserMessage) object;
            System.out.println("接收到的消息是：" + tum);
            String type = tum.getType();
            switch (type) {
                case "managerCheck":
                    ManagerChk(tum);
                    break;
                case "addUser":
                    AddUser(tum);
                    break;
                case "deleteUser":
                    DeleteUser(tum);
                    break;
                case "select":
                    SelectUser(tum);
                    break;
                case "update":
                    UpdateUser(tum);
                    break;
                case "getAll":
                    GetAllUsers(tum);
                    break;
                case "userLogin":
                    userLogin(tum);
                    break;
                case "updatePassword":
                    userUpdatePassword(tum);
                    break;
                case "signOut":
                    signOut(tum);
                    return false;
                default:
                    break;
            }
        } else {
            ExamMessage tum = (ExamMessage) object;
            System.out.println("接收到的消息是：" + tum);
            String type = tum.getType();
            switch (type) {
                case "addExam":
                    AddExam(tum);
                    break;
                case "deleteExam":
                    DeleteExam(tum);
                    break;
                case "selectExam":
                    SelectExam(tum);
                    break;
                case "updateExam":
                    UpdateExam(tum);
                    break;
                case "getAllExams":
                    GetAllExams(tum);
                    break;
                case "startExam":
                    startExam(tum);
                    break;
                default:
                    break;
            }
        }
        return true;

    }


    public void userUpdatePassword(UserMessage tum) throws IOException {
        List<User> list = new ArrayList<>();
        if (null != sd.serverUpdatePassword(tum.getList())) {
            list.add(sd.serverUpdatePassword(tum.getList()));
            tum.setType("success");
            tum.setList(list);
        } else {
            tum.setType("fail");
        }
        sic.getOos().writeObject(tum);
        System.out.println("服务器修改学员密码成功！");
    }

    public void userLogin(UserMessage tum) throws IOException {
        User user = sd.serverUserLogin(tum.getList());
        List<User> list = new ArrayList<User>();
        if (null != user) {
            list.add(user);
            tum.setList(list);
            tum.setType("success");
        } else {
            tum.setList(null);
            tum.setType("fail");
        }
        sic.getOos().writeObject(tum);
        System.out.println("服务器处理学员登录系统成功！");
    }

    public void GetAllUsers(UserMessage tum) throws IOException {
        List<User> list = sd.serverGetUserList();
        if (list == null) {
            tum.setType("fail");
            tum.setList(list);
        } else {
            tum.setType("success");
            tum.setList(list);

        }
        sic.getOos().writeObject(tum);
        System.out.println("服务器处理获取学员信息成功！");
    }

    public void UpdateUser(UserMessage tum) throws AgeException, IOException {
        List<User> list = new ArrayList<>();
        User user = sd.serverUpdateUser(tum.getList());
        list.add(user);
        if (null != user) {
            tum.setType("success");
            tum.setList(list);
        } else {
            tum.setType("fail");
            tum.setList(list);
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器修改学员信息成功！");
    }


    public void SelectUser(UserMessage tum) throws IOException {
        List<User> list = new ArrayList<>();
        User user = sd.serverSelectUser(tum.getList());
        list.add(user);
        if (null != user) {
            tum.setType("success");
            tum.setList(list);
        } else {
            tum.setType("fail");
            tum.setList(list);
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送查找学员信息成功！");
    }


    public void DeleteUser(UserMessage tum) throws IOException {
        if (sd.serverDeleteUser(tum.getList())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送删除学员信息成功！");
    }

    /**
     * 添加学员并将结果发送给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void AddUser(UserMessage tum) throws IOException {
        if (sd.serverAddUser(tum.getList())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送添加学员成功！");
    }


    /**
     * 校验管理员账号密码信息并发送给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void ManagerChk(UserMessage tum) throws IOException {
        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(tum.getList())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送校验结果成功！");
    }

    /**
     * 返回添加考题给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void AddExam(ExamMessage tum) throws IOException {
        if (sd.serverAddExam(tum.getList())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送添加考题成功！");
    }

    /**
     * 返回删除考题给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void DeleteExam(ExamMessage tum) throws IOException {
        if (sd.serverDeleteExam(tum.getList())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送删除考题成功！");
    }

    /**
     * 返回查找考题给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void SelectExam(ExamMessage tum) throws IOException {
        List<Exam> list = new ArrayList<>();
        Exam exam = sd.serverSelectExam(tum.getList());
        list.add(exam);
        if (null != exam) {
            tum.setType("success");
            tum.setList(list);
        } else {
            tum.setType("fail");
            tum.setList(list);
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送查找考题成功！");
    }


    /**
     * 返回更新考题给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void UpdateExam(ExamMessage tum) throws IOException {
        List<Exam> list = new ArrayList<>();
        Exam exam = sd.serverUpdateExam(tum.getList());
        list.add(exam);
        if (null != exam) {
            tum.setType("success");
            tum.setList(list);
        } else {
            tum.setType("fail");
            tum.setList(list);
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器修改题目信息成功！");
    }

    /**
     * 返回考题给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void GetAllExams(ExamMessage tum) throws IOException {
        List<Exam> list = sd.serverGetExamList();
        if (list == null) {
            tum.setType("fail");
            tum.setList(list);
        } else {
            tum.setType("success");
            tum.setList(list);

        }
        sic.getOos().writeObject(tum);
        System.out.println("服务器处理获取考题成功！");

    }

    /**
     * 返回分数给客户端
     *
     * @param tum
     * @throws IOException
     */
    public void startExam(ExamMessage tum) throws IOException {
        int grade = sd.serverStartExam(tum.getList());
        tum.setType(grade + "");
        tum.setList(null);
        sic.getOos().writeObject(tum);
        System.out.println("服务器返回分数成功！");
    }


    /**
     * 服务器将学员信息保存到本地，并告诉客户端退出，同时服务器也退出
     * @param tum
     * @throws IOException
     */
    public void signOut(UserMessage tum) throws IOException {
        if (sd.outputUser(tum.getType())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        sic.getOos().writeObject(tum);
        System.out.println("服务器返回输出到本地信息成功！");
        sic.serverClose();
    }

    public void input() {
        sd.inputUser();
    }

}
